var JPush = require('jpush-sdk');
var CONSTS = require('../consts');
var logger = require('../models/logger');

var jpushHelper = {    
    //调用jpush发送消息推送
    //alias:接收人别名(用户_id)数组
    //alertmsg:推送消息的标题
    //alerttitle:推送消息的标题
    //contentmsg:推送消息的内容
    //extra:而外信息
    pushMessage: function(alias, alertmsg, alerttitle, contentmsg, extra) {
        logger.debug("alias:",  alias,  " alertmsg:" + alertmsg);
        if(!alias || !alertmsg) {
            return logger.debug('parameter not finish');
        }

        var client = JPush.buildClient(CONSTS.JPUSH.APPKEY, CONSTS.JPUSH.SECRET);
        var msgContent = extra?JSON.stringify(extra):'';

        client.push().setPlatform(JPush.ALL)
            .setAudience(JPush.alias(alias))
            .setNotification(CONSTS.APP.TITLE, JPush.ios(alertmsg, alerttitle, 5, extra), JPush.android(alertmsg, alerttitle, 5, extra))
            .setMessage(msgContent)
            .send(function(err, res) {
                if (err) {
                    logger.debug('err message:', err.message);
                } else {
                    logger.debug('Sendno: ' + res.sendno, ' msg_id:', res.msg_id);
                }
            });        
    },
};

module.exports = jpushHelper;    